package com.itboye.api.domain;

import com.github.pagehelper.Page;
import com.itboye.api.helper.ParamsHelper;
import com.itboye.api.model.ByResponseData;
import com.itboye.mybatis.helper.ByPageInfoHelper;
import com.itboye.mybatis.model.BannersModel;
import com.itboye.services.utils.interfaces.IBannersService;
import com.itboye.system.exception.ByBaseException;
import com.itboye.system.helper.ByConsoleHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;

/**
 * BannersDomain.java
 * Created on  2016/8/9 12:05
 * 版本       修改时间          作者      修改内容
 * V1.0.1    2016/8/9      hebidu    初始版本
 * <p>
 * Copyright (c) 2013 杭州博也网络科技有限公司 版权所有
 * HANGZHOU BOYE TECHNOLOGY CO.,LTD. All Rights Reserved.
 */
@Component("BannersDomain")
public class BannersDomain extends BaseDomain {

    private static final Logger logger = LogManager.getLogger(BannersDomain.class.toString());

    @Resource
    private IBannersService bannersService;

    public BannersDomain() {
        super();
        ByConsoleHelper.debug("BannersDomain init", logger);
    }

    /**
     * 分页查询
     * @return
     */
    public ByResponseData query() throws ByBaseException, UnsupportedEncodingException {
        String position = this.getStringParam("position");
        Integer pageNum = this.getIntegerParam("page_num");
        Integer pageSize = this.getIntegerParam("page_size");
        ByConsoleHelper.debug("position="+position,logger);

        assertNotNull(position,"position  参数缺失");
        assertNotNull(pageNum, "page_num  参数缺失");
        assertNotNull(pageSize,"page_size 参数缺失");

        HashMap<String,String> params = ParamsHelper.getNewHashMap();
        params.put("position",position);

        String fields = null;
        String orders = "createtime desc";

        ByPageInfoHelper<BannersModel> page =  bannersService.queryByPaging(params,pageNum,pageSize,fields,orders);

        if(page != null){
            return success(page);
        }

        return fail("查询失败");
    }

}
